package com.wangsd.web.mircomerchant.utils;

import com.alibaba.fastjson.JSONObject;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;

public class MapSortUtil {

    public static void main(String[] args) {

        Map<String, String> map = new TreeMap<String, String>();

        map.put("KFC", "kfc");
        map.put("WNBA", "wnba");
        map.put("NBA", "nba");
        map.put("CBA", "cba");

        Map<String, String> resultMap = sortMapByKey(map);    //按Key进行排序

        for (Map.Entry<String, String> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, String> sortMap = new TreeMap<String, String>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }

    public static  String xml2JSON(String xml) {
        JSONObject obj = new JSONObject();
        try {
            InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(is);
            Element root = doc.getRootElement();
            obj.put(root.getName(), iterateElement(root));

            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static Map  iterateElement(Element element) {
        List jiedian = element.getChildren();
        Element et = null;
        Map map = new HashMap();
        List list = null;
        //增加了对节点的处理
        List<Attribute> attributes=element.getAttributes();
        for(int i=0;i<attributes.size();i++){
            map.put(attributes.get(i).getName(), attributes.get(i).getValue());
        }
        //遍历所有子节点
        for (int i = 0; i < jiedian.size(); i++) {
            list = new LinkedList();
            et = (Element) jiedian.get(i);
            //若  子节点内文本为空
            if (et.getTextTrim().equals("")) {
                if (et.getChildren().size() == 0)
                    //若  没有子节点则继续
                    continue;
                //若有  子节点  且  当前map有 本节点名称
                if (map.containsKey(et.getName())) {
                    list.add(map.get(et.getName()));
                    //list = (List) map.get(et.getName());
                }
                list.add(iterateElement(et));
                if(list.size()>1){
                    map.put(et.getName(), list);
                }else{
                    map.put(et.getName(), list.get(0));
                }

            } else { //子节点内 文本不为空
                List<Attribute> temp_att=et.getAttributes();
                //若  子节点内有属性
                if(temp_att.size()!=0){
                    Map childMap=new HashMap();
                    for(int j=0;j<temp_att.size();j++){
                        childMap.put(temp_att.get(j).getName(), temp_att.get(j).getValue());
                    }
                    childMap.put("content",et.getTextTrim());
                    map.put(et.getName(), childMap);
                }else{
                    if (map.containsKey(et.getName())) {

                        try{
                            list = (List) map.get(et.getName());
                        }catch(Exception e){
                            list.add(map.get(et.getName()));
                        }
                    }
                    list.add(et.getTextTrim());
                    if(list.size()>1){
                        map.put(et.getName(), list);
                    }else{
                        map.put(et.getName(), list.get(0));
                    }
                }


            }
        }
        return map;
    }
}

class MapKeyComparator implements Comparator<String> {

    @Override
    public int compare(String str1, String str2) {

        return str1.compareTo(str2);
    }
}